package com.li.binaryaeetree;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class te {
    public static void main(String[] args) {
        lengthOfLongestSubstring("sdvcxcxvc");
    }

        public  static int lengthOfLongestSubstring(String s) {
          /*  int n = s.length(), ans = 0;
            Map<Character, Integer> map = new HashMap<>();
            for (int end = 0, start = 0; end < n; end++) {
                char alpha = s.charAt(end);
                if (map.containsKey(alpha)) {
                    start = Math.max(map.get(alpha), start);
                }
                ans = Math.max(ans, end - start + 1);
                map.put(s.charAt(end), end + 1);
            }
            return ans;*/
            int left=0,n=s.length(),res=0;
            Map<Character,Integer> map = new HashMap<Character,Integer>();
            for (int i=0;i<n;++i){
                char chr = s.charAt(i);
                if (map.containsKey(chr)){
                    //更新左边界
                    left = Math.max(left,map.get(s.charAt(i))+1);
                }
                map.put(chr,i);
                res = Math.max(res,i-left+1);
            }
            return res;
        }
    }

/*    作者：guanpengchn
    链接：https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/hua-jie-suan-fa-3-wu-zhong-fu-zi-fu-de-zui-chang-z/
    来源：力扣（LeetCode）
    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。*/

